/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import Entity.Administrator;
import dbConnect.jdbcConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class adaptecAdmin {

    Connection con = null;

    public adaptecAdmin() throws Exception {
        con = jdbcConnection.getConnection();
    }
    public ArrayList<Administrator> getAllAdministrator() {
        try {
            ArrayList<Administrator> listAdmin = new ArrayList<Administrator>();
            PreparedStatement pr = con.prepareStatement("select*from Administrator");
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Administrator admin=new Administrator();
                admin.setUserName(rs.getString(1));
                admin.setPasswords(rs.getString(2));
                admin.setFullName(rs.getString(3));
                admin.setAddress(rs.getString(4));
                listAdmin.add(admin);
                
            }
            return listAdmin;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    //method search admin

    public ArrayList<Administrator> searchAdministrator(String userName) {
        try {
            ArrayList<Administrator> listAdmin = new ArrayList<Administrator>();
            PreparedStatement pr = con.prepareStatement("select*from Administrator where userName =?");
            pr.setString(1,userName);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Administrator admin=new Administrator();
                admin.setUserName(rs.getString(1));
                admin.setPasswords(rs.getString(2));
                admin.setFullName(rs.getString(3));
                admin.setAddress(rs.getString(4));
                listAdmin.add(admin);
            }
            return listAdmin;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
//method insert admin

    public boolean insertAdministrator(String userName,String passwourd,String fullName,String address) {
        try {
            PreparedStatement pr = con.prepareStatement("insert into Administrator values(?,?,?,?)");
            pr.setString(1, userName);
            pr.setString(2, passwourd);
            pr.setString(3,fullName);
            pr.setString(4, address);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method update admin

    public boolean updateAdminstrator(String passwourd,String fullName,String address,String userName) {
        try {
            PreparedStatement pr = con.prepareStatement("update Administrator set passwords=?,fullName=?,address=? where userName=?");       
            pr.setString(1, passwourd);
            pr.setString(2,fullName);
            pr.setString(3, address);
            pr.setString(4, userName);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //method delete admin

    public boolean deleteAdministrator(String name) {
        try {
            PreparedStatement pr = con.prepareStatement("delete Administrator  where userName=?");
            pr.setString(1, name);
            int result = pr.executeUpdate();
            if (result > 0) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
    //
    public boolean checkLoginAdministrator(String name,String pass) {
        try {
            PreparedStatement pr = con.prepareStatement("select*from Administrator where userName=? and passwords=?");
            pr.setString(1, name);
            pr.setString(2, pass);
            ResultSet result = pr.executeQuery();
            if (result.next()) {
                return true;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

}

